//
// Created by zhangzhiwen on 25-5-24.
//

#include "../Inc/distance.h"

#include <stdio.h>
#include <math.h>


// 计算向量的叉积
Point3D cross_product(Point3D a, Point3D b)
{
    Point3D result;
    result.x = a.y * b.z - a.z * b.y;
    result.y = a.z * b.x - a.x * b.z;
    result.z = a.x * b.y - a.y * b.x;
    return result;
}

// 计算向量的模（长度）
double vector_length(Point3D v)
{
    return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}

// 计算点到直线的距离
double point_to_line_distance(Point3D point, Point3D line_point, Point3D line_direction)
{
    Point3D pq; // 从直线上的点到目标点的向量
    pq.x = point.x - line_point.x;
    pq.y = point.y - line_point.y;
    pq.z = point.z - line_point.z;

    // 计算叉积 pq × line_direction
    Point3D cross = cross_product(pq, line_direction);

    // 距离 = |pq × line_direction| / |line_direction|
    return vector_length(cross) / vector_length(line_direction);
}

